<?php

namespace app\admin\controller\repertory;

use app\admin\controller\Controller;
use app\admin\model\repertory\Store;
use app\admin\model\repertory\User as Models;
use app\common\model\repertory\City;
use app\common\model\repertory\Dept;
use app\common\model\repertory\User as RepertoryUser;

/**
 * 用户
 */
class User extends Controller
{
    /**
     * 列表
     */
    public function index()
    {
        $list = Models::getAll($this->store_id);
        return $this->renderSuccess('', compact('list'));
    }

    /**
     * 添加
     */
    public function add()
    {
        $model = new Models;
        // 新增记录
        if ($model->add($this->postData(), $this->store_id)) {
            return $this->renderSuccess('追加成功');
        }
        return $this->renderError('失敗を追加');
    }

    /**
     * 更新权限
     */
    public function edit()
    {
        // 权限详情
        $data = $this->postData();
        $model = Models::detail($data['id']);

        // 更新记录
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError('更新失敗');
    }

    /**
     * 删除小程序
     */
    public function delete()
    {
        $message_id = $this->request->param('id');
        // 小程序详情
        $model = Models::detail($message_id);
        if (!$model->setDelete()) {
            return $this->renderError('操作に失敗');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 获取地区
     */
    public function getCity()
    {
        $data = City::where('is_delete', '=', 0)->order(['sorts' => 'asc'])->select();
        return $this->renderSuccess('', $data);
    }

    /**
     * 超管后台登录
     */
    public function login()
    {
        $model = new Models;
        if ($user = $model->login($this->postData())) {
            return $this->renderSuccess('登录成功', [
                'user_name' => $user['user_name'],
                'token' => $user['token']
            ]);
        }
        return $this->renderError('用户名或者密码错误！');
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        $token = Request()->header('token');
        Cache::delete('admin_token_' . $token);
        return $this->renderSuccess('退出成功');
    }

    /**
     * 接口创建用户
     */
    public function portUserInfo()
    {
        $data = $this->postData();
        $deptData = [
            "title" => $data["app_name"],
            "top_title" => $data["app_name"],
            "create_time" => time(),
            "update_time" => time(),
        ];
        $res = Dept::insertGetId($deptData);
        if ($res && $res > 0) {
            $store_id = Store::insertGetId([
                "title" => $data["app_name"],
                "create_time" => time(),
                "update_time" => time(),
            ]);
            if ($store_id && $store_id > 0) {
                $store_id = $store_id;
            } else {
                $store_id = 0;
            }
            RepertoryUser::insert([
                "store_id" => $store_id,
                "dept_id" => $res,
                "name" => $data["app_name"],
                "username" => $data["user_name"],
                "password" => salt_hash($data["password"]),
                "tpassword" => $data["password"]
            ]);
        }
        return $this->renderSuccess('操作成功');
    }
}
